public without sharing class WorkOrderPartsComponentController {
    
    public String msg {private set ;get;}
	public String backId {private set; get;}
	public String dataJson {private set; get;}
    public String saveJson { set; get;}

    public  WorkOrder__c wo {get;private set;}
	public Boolean isSuc {private set; get;}
	public Boolean isErr {private set; get;}

    public WorkOrderPartsComponentController() {
        
    	 backId = System.currentPageReference().getParameters().get('id');


    }


    public void init(){

    	try {
    		if(backId == null )throw new DmlException('业面打开错误!');
    		
    		wo = [select Id ,Account__c,Status__c,ManufactureInfo__c,ManufactureInfo__r.NCCode__c  from WorkOrder__c where Id =:backId];


    		if(wo.Status__c == '完成') throw new DmlException('工单已完成!');
            dataJson = EncodingUtil.urlEncode(JSON.serialize(getDetails()), 'UTF-8');

    	
    	}catch (Exception e) {
    	    isErr = true;
            ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR ,e.getMessage()));
    	}
    	
    }

    public List<Object> getDetails(){

    	List<Object> tmp = new List<Object>();
    	for (Kit__c k  :[select Product__r.ProductName__c,Product__r.NCCode__c,FuselageNumber__c from Kit__c where WorkOrder__c =:backId ] ) {
    		tmp.add(new KitBean(k));
    	}
    	return tmp;

    }

    public void save(){

    	try{

    		List<Object> saveList = (List<Object>) JSON.deserializeUntyped(EncodingUtil.urlDecode(saveJson, 'UTF-8'));
	    	Set<String> products = new Set<String>();
	    	List<Kit__c>  saL = new List<Kit__c>();

	    	for (Object o  : saveList ) {
	    		Map<String,Object> mo = (Map<String,Object>)o;
	    		String nccode = DataUtil.getStringValue(mo.get('PARTINVCODE')); 
	    	}

	    	Map<String,Id> pM = new Map<String,Id>();
	    	for (Product__c p  :[select Id ,NCCode__c from Product__c where NCCode__c in:products and RecordType.DeveloperName = 'NCProducts'] ) {
	    		pM.put(p.NCCode__c, p.Id);
	    	}
	    	for (Object o  : saveList ) {
	    		Map<String,Object> mo = (Map<String,Object>)o;
	    		Kit__c k = new Kit__c();
	    		String nccode = DataUtil.getStringValue(mo.get('PARTINVCODE')); 
	    		k.FuselageNumber__c = DataUtil.getStringValue(mo.get('ENO'));
	    		k.Account__c = wo.Account__c;
	    		k.WorkOrder__c = wo.Id;
	    		k.Product__c = pM.get(nccode);
	    		if(k.Product__c == null ) throw new DmlException('没有找到SF产品:'+nccode);
	    		saL.add(k);
	    	}

	    	
    		SavePoint sp = Database.setSavepoint();
    		try{

    			wo.isAddKit__c = saL.size() >0 ; 
    			delete [select Id from Kit__c where WorkOrder__c =: wo.Id];
    			insert saL;
    			msg = '保存成功!';
    			isSuc = true;
	    	}catch(Exception e ){
	    		Database.rollback(sp);
	    		throw e ;
	    	}
    	}catch(Exception e ){
    		msg = '保存失败:'+e.getMessage();
    	}
    	

    }
    public class KitBean{

    	public String PARTINVNAME;
    	public String PARTINVCODE;
    	public String ENO;
    	public KitBean(Kit__c k ){
    		PARTINVNAME = k.Product__r.ProductName__c;
    		PARTINVCODE  = k.Product__r.NCCode__c;
    		ENO = k.FuselageNumber__c;

    	}
    }
    


}